package logic.enemies;

import java.util.ArrayList;

import logic.blocks.Block;
import logic.characters.Crash;
import logic.characters.Jigglypuff;
import logic.characters.Player;
import logic.characters.Samus;
import logic.characters.Snake;
import logic.pruebas.GamePanel;
import logic.threads.ShootBullet;

public class Soldier extends Enemy{
	private static final long serialVersionUID = 549113290763692587L;
	private long lastActionTime = System.currentTimeMillis();
	private boolean shooting = false;

	public Soldier(float x, float y, GamePanel panel, String nomImg,
			int difficulty, ArrayList<Enemy> enemies, ArrayList<Block> blocks, Player player) {
		super(x, y, panel, nomImg, difficulty, enemies, blocks, player);
		attack = 5;
		defense = 4;
		magic = 17;
		resistance = 13;
		experience = 3;
		life = 28;
		jump = 3;
		speed = 7;
		life = life * difficulty;
		attack = attack * difficulty;
		defense = defense * difficulty;
		magic = magic * difficulty;
		resistance = resistance * difficulty;
		originalSpeed = speed;
		originalJump = jump;
		experience = experience * difficulty;
	}

	@Override
	public void upImpact(Player player) {
		if ((player instanceof Snake || player instanceof Samus) && player.special) {
			player.specialEffect(this);
		} else {
		player.damage(attack);
		jump();
		}
		
	}

	@Override
	public void downImpact(Player player) {
		if ((player instanceof Snake || player instanceof Samus) && player.special) {
			player.specialEffect(this);
		} else {
		player.falling = false;
		player.onPlatform = true;
		player.velY = 0;
		player.coorY = coorY - player.altura - 1;
		player.jumping= false;
		player.jump();
		}
		
	}

	@Override
	public void rightImpact(Player player) {
		if (player.attacking && player.lookingRight) {
			damage(player.attack);
			velX = -velX;
			coorX = player.collisionX + player.collisionWidth + 1;
		} else if (player.special){
			if (player instanceof Crash || player instanceof Jigglypuff || player instanceof Samus || player instanceof Snake) {
				if (player instanceof Snake) {
					player.specialEffect(this);
				} else {
					coorX = player.collisionX + player.collisionWidth + 1;
					player.specialEffect(this);
				}
			} else if (player.lookingRight) {
				coorX = player.collisionX + player.collisionWidth + 1;
				player.specialEffect(this);
			}

		} else {
			player.damage(attack);
			velX = -velX;
			player.velX = 0;
			coorX = player.collisionX + player.collisionWidth + 1;
		}
		//jugador.velX = 0;
		
		
	}

	@Override
	public void leftImpact(Player player) {
		if (player.attacking && !player.lookingRight) {
			damage(player.attack);
			velX = -velX;
			coorX = player.collisionX - collisionWidth - 1;
		} else if (player.special) {
			if (player instanceof Crash || player instanceof Jigglypuff || player instanceof Samus || player instanceof Snake) {
				if (player instanceof Snake) {
					player.specialEffect(this);	
				} else {
					coorX = player.collisionX - collisionWidth - 1;
					player.specialEffect(this);
				}
			} else if (!player.lookingRight) {
				coorX = player.collisionX - collisionWidth - 1;
				player.specialEffect(this);
			}
		} else {
			player.damage(attack);
			velX = -velX;
			player.velX = 0;
			coorX = player.collisionX - collisionWidth - 1;
		}
		//jugador.velX = 0;
		
	}
	
	public void update(Player player) {
		if (!dead && !sleeping && relevant) {
		if (shooting) {
			endRangedAttack();
		} else if (!shooting) {
			if (Math.random() > 0.9998) {
				if (velX < 0) {
					lookingRight = true;
				} else if (velX > 0) {
					lookingRight = false;
				}
				rangedAttack();
			}
			if (velX == 0) {
				if (player.collisionX < coorX && !shooting) {
					updateIcon("resources/soldierIL.gif");
					lookingRight = false;
				} else if (player.collisionX > coorX && !shooting) {
					updateIcon("resources/soldierIR.gif");
					lookingRight = true;
				}
			}

			if (player.collisionX - coorX < 0 && player.collisionX - coorX > -250 && !shooting) {
				velX = speed * 20;
				updateIcon("resources/soldierRR.gif");
			} else if (player.collisionX - coorX > 0 && player.collisionX - coorX < 250 && !shooting) {
				velX = -speed * 20;
				updateIcon("resources/soldierRL.gif");
			}

			if (player.collisionX - coorX < -250) {
				velX = 0;
			} else if (player.collisionX - coorX > 250) {
				velX = 0;
			}
		}
		}
		super.update(player);
	}
	
	public void shoot() {
		Runnable shootBullet = new ShootBullet(collisionX, collisionY + 10, panelJuego, lookingRight, enemies, blocks, magic, false, player, 500);
		Thread fThread = new Thread(shootBullet);
		fThread.start();
	}

	public void rangedAttack() {
		velX = 0;
		shooting = true;
		if (lookingRight) {
			updateIcon("resources/soldierSR.gif");
		} else {
			updateIcon("resources/soldierSL.gif");
		}
		dibujar();

		lastActionTime = System.currentTimeMillis();
		shoot();			
	}
	
	public void endRangedAttack() {
		long now = System.currentTimeMillis();

		if ((now - lastActionTime) >= 1000) {
			if (lookingRight) {
				updateIcon("resources/soldierIR.gif");
				dibujar();
			} else {
				updateIcon("resources/soldierIL.gif");
				dibujar();
			}
			shooting = false;
		}
		
	}


}
